package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.knowledge.domain.EnterpriseFilesTree;
import cn.cdeden.knowledge.service.EnterpriseFilesTreeService;
import cn.cdeden.knowledge.service.EnterprisePermService;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.RemoteFileService;
import cn.cdeden.resource.api.domain.*;
import cn.cdeden.system.api.domain.enums.EnterprisePermTypeEnum;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author username
 * @date 2024/12/16 17:48
 * @describe:
 **/
@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "dify", version = "1.0.0")
public class OssUploadDifyFileServiceImpl implements OssUploadService {
    private final EnterpriseFilesTreeService enterpriseFilesTreeService;
    private final EnterprisePermService permissionService;
    @DubboReference(check = false)
    private final RemoteFileService remoteFileService;
    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        EnterpriseFilesTree byId = enterpriseFilesTreeService.lambdaQuery().eq(EnterpriseFilesTree::getFileDatasetId, moduleId).one();
        if (byId == null)throw new RuntimeException("该文件在企业库中找不到文件");
        Boolean b = permissionService.checkPermission(byId.getId(), EnterprisePermTypeEnum.DOWNLOAD, userId);
        RemoteFile remoteFile = new RemoteFile();
        remoteFile.setOssId(byId.getStorageId() == null ? null : Long.valueOf(byId.getStorageId()));
        remoteFile.setVersionId(byId.getEdition());
        remoteFile.setVersionName(byId.getEditionName());
        remoteFile.setFileSize(byId.getSize());
        remoteFile.setFileSuffix(byId.getSuffix());
        remoteFile.setName(byId.getRealName());
        remoteFile.setOriginalName(byId.getRealName());
        remoteFile.setModuleType(OssModuleType.Enterprise.getCode());
        remoteFile.setPrint(false);
        remoteFile.setRead(true);
        remoteFile.setEdit(false);
        remoteFile.setDownload(b);
        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId) {
        return new RemoteOssUpload();
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        return null;
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        List<EnterpriseFilesTree> filesTrees1 = enterpriseFilesTreeService.lambdaQuery().in(EnterpriseFilesTree::getFileDatasetId, idList).list();
        List<RemoteFilesDownload> treeList = new ArrayList<>();
        filesTrees1.forEach( v -> {
            treeList.add(RemoteFilesDownload.builder()
                .id(v.getId().toString())
                .storageId(v.getStorageId())
                .dateId(v.getId().toString())
                .parentId(idList.contains(v.getId().toString()) ? null : v.getParentId())
                .fileSize(v.getSize())
                .fileName(v.getRealName())
                .fileSuffix(v.getSuffix())
                .isFolder(v.getType().equals("folder"))
                .versionId(v.getEdition())
                .versionName(v.getEditionName())
//                .isFolder(v.getLibraryType().equals("folder"))
                .build());
        });
        return treeList;
    }
}
